package com.qjcc.ui;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;

import com.qjcc.R;
import com.qjcc.util.SharedPreferencesUtil;
import com.qjcc.util.UIHelper;

/**
 * 待办审批列表通用
 * Created by loveasia on 2016/7/8.
 */
public class TodoCommonActivity extends Activity {
    /**
     * 列表展示页面
     */
    private WebView webView;
    /**
     * 待审核按钮
     */
    private Button toApproveBt;
    /**
     * 已审核按钮
     */
    private Button approvedBt;
    /**
     * 返回按钮
     */
    private ImageButton backImage;

    /**
     * 审批类型 1--会议  2--访客   3--车辆  4--其它
     */
    private String type;
    /**
     * 页面地址
     */
    private String url;
    /**
     * 暂无数据视图
     */
    private LinearLayout list_no_result;
    /**
     * 页面顺序
     */
    private int pageIndex = 1;

    private boolean isError = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_index);
        init();
    }

    /**
     * 初始华数据
     */
    public void init(){
        webView = (WebView)findViewById(R.id.webView);
        setWebView(webView);
        setUrl("0");
        //默认加载待审批页面
        webView.loadUrl(url);

    }

    /**
     * 设置url连接
     */
    public void setUrl(String status){
//        String userId = SharedPreferencesUtil.getLoginUserId(TodoCommonActivity.this);
        if("1".equals(type)){
//            url = getString(R.string.url_meeting);
        }else if("2".equals(type)){
            url = "";
        }else if("3".equals(type)){
            url = "";
        }else if("4".equals(type)){
            url = "";
        }
        url = "https://www.baidu.com";
    }

    public void setHead(){

    }


    /**
     * 设置按钮监听
     * @param btn
     * @return
     */
    private View.OnClickListener btnClickListener(final Button btn) {
        return new View.OnClickListener() {
            public void onClick(View v) {
                /*//0为待审批，1为已审批
                if (btn == toApproveBt) {
                    toApproveBt.setEnabled(false);
                    toApproveBt.setTextColor(getResources().getColor(R.color.home_head_bg));
                    approvedBt.setEnabled(true);
                    approvedBt.setTextColor(getResources().getColor(R.color.white));
                    //加载待审核页面
                    setUrl("0");
                    webView.loadUrl(url);
                } else {
                    toApproveBt.setEnabled(true);
                    toApproveBt.setTextColor(getResources().getColor(R.color.white));
                    approvedBt.setEnabled(false);
                    approvedBt.setTextColor(getResources().getColor(R.color.home_head_bg));
                    //加载已审核页面
                    setUrl("1");
                    webView.loadUrl(url);
                }*/
            }
        };
    }


    @Override
    public void onBackPressed() {
        backPressed();
    }

    /**
     * 返回事件
     */
    public void backPressed(){
        if (webView.canGoBack() && pageIndex != 1) {
            webView.goBack();
        } else {
            super.onBackPressed();
        }
    }

    private void setWebView(final WebView todoWebView) {
        WebSettings webSettings = todoWebView.getSettings();
        webSettings.setSavePassword(false);
        webSettings.setSaveFormData(false);
        webSettings.setJavaScriptEnabled(true);
        webSettings.setAllowFileAccess(true); // 设置允许访问文件数据
        webSettings.setSupportZoom(false);
        webSettings.setBuiltInZoomControls(true);
        webSettings.setAppCacheEnabled(true);
        todoWebView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(android.webkit.WebView view, String url) {
                view.loadUrl(url);
                return true;
            }

            @Override
            public void onPageStarted(android.webkit.WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
            }

            @Override
            public void onPageFinished(android.webkit.WebView view, String url) {
                super.onPageFinished(view, url);
                if (!isError) {
                    view.setVisibility(View.VISIBLE);
//                    list_no_result.setVisibility(View.GONE);
                }
            }

            @Override
            public void onReceivedError(android.webkit.WebView view, int errorCode, String description, String failingUrl) {
                isError = true;
                view.setVisibility(View.GONE);
//                list_no_result.setVisibility(View.VISIBLE);
                UIHelper.ToastMessage(TodoCommonActivity.this, description);
                super.onReceivedError(view, errorCode, description, failingUrl);
            }
        });
        todoWebView.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (event.getAction() == KeyEvent.ACTION_DOWN) {
                    if (keyCode == KeyEvent.KEYCODE_BACK && todoWebView.canGoBack()) {  //表示按返回键  时的操作
                        todoWebView.goBack();   //后退
                        return true;    //已处理
                    }
                    if (keyCode == KeyEvent.KEYCODE_FORWARD && todoWebView.canGoForward()) {
                        todoWebView.goForward();
                        return true;
                    }
                }
                return false;
            }
        });
    }
}
